package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.InoculateContract;
import com.xuxin.baby.popup.ConfirmPopup;
import com.xuxin.baby.presenter.InoculatePresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.DatePickerUtils;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.27 0027
 * 疫苗接种
 */
public class ChildInoculateActivity extends BaseActivity implements InoculateContract.View {
    private final InoculateContract.Presenter presenter = new InoculatePresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.introduce_name)
    TextView introduce_name;//疫苗名称
    @BindView(R.id.introduce_time)
    TextView introduce_time;//接种时间
    @BindView(R.id.introduce_protect)
    TextView introduce_protect;//可预防传染病
    @BindView(R.id.introduce_attention)
    TextView introduce_attention;//注意事项
    @BindView(R.id.introduce_introduce)
    TextView introduce_introduce;//疫苗介绍
    @BindView(R.id.introduce_address)
    EditText introduce_address;

    private String tenantId;//租户id
    private String token;//授权token
    private String childId;//宝宝id
    private String vaccineId;
    private String hospital;
    private String inoculateTime;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_inoculate);
    }

    @Override
    public void initView() {
        button.setText("保存");
        vaccineId = getIntent().getStringExtra("vaccineId");
        String vaccineName = getIntent().getStringExtra("vaccineName");
        String vaccinePrevent = getIntent().getStringExtra("vaccinePrevent");
        String vaccineAttention = getIntent().getStringExtra("vaccineAttention");
        String vaccineIntroduction = getIntent().getStringExtra("vaccineIntroduction");
        title_name.setText(vaccineName);
        introduce_name.setText(vaccineName);
        introduce_protect.setText(vaccinePrevent);
        introduce_attention.setText(vaccineAttention);
        introduce_introduce.setText(vaccineIntroduction);
        introduce_time.setText(TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy-MM-dd")));
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        childId = aCache.getAsString(BaseConstant.childId);
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.title_left, R.id.introduce_time,
            R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.introduce_time) {
            DatePickerUtils.showDate(context, new DatePickerUtils.onListener() {
                @Override
                public void onShow(String time) {
                    introduce_time.setText(time);
                }
            });
        } else if (id == R.id.button) {
            hospital = introduce_address.getText().toString();
            inoculateTime = introduce_time.getText().toString();
            if (StringUtils.isEmpty(hospital)) {
                ToastUtils.showShort("请填写接种医院名称");
                return;
            }
            if (StringUtils.isEmpty(inoculateTime)) {
                ToastUtils.showShort("请选择接种时间");
                return;
            }
            new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(true)
                    .asCustom(new ConfirmPopup(context, "确定要记录接种疫苗吗？", new ConfirmPopup.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            try {
                                popup.show();
                                handler.sendEmptyMessage(0);
                            } catch (Exception e) {
                                e.printStackTrace();
                                dismiss();
                            }
                        }
                    })).show();
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NotNull Message msg) {
            if (msg.what == 0) {
                presenter.inoculate(tenantId, token, childId, vaccineId, hospital, inoculateTime);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        setResult(200);
        finish();
    }
}